home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / tkern091.zip / SRC / PUTENV.C < prev    next >
Text File  |  1994-02-26  |  2KB  |  84 lines

  1. /*
  2.  * putenv.c - Replacement for broken putenv included in the BC++4.0 libraries
  3.  *
  4.  * Copyright (C) 1994 Troy Rollo.
  5.  *
  6.  * This file is explicitly placed in the public domain. It may be used for any purpose,
  7.  * including, but not limited to, incorperation into commercial, shareware, freeware,
  8.  * public domain or free software works, without fee of any kind being payable to
  9.  * the author of this work, and without requirement for any credits or notice to be
  10.  * attached to the work, provided this copyright notice remains intact, and provided
  11.  * that if any changes are made, that a notice outlining the changes made be included
  12.  * in this source code.
  13.  *
  14.  * This file is provided without any warranty of any kind, express or implied. The user
  15.  * assumes all risks related to its use.
  16.  */
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20.  
  21. extern    char    **_environ;
  22. extern    unsigned _envSize;
  23. int
  24. putenv(char const *pchVar)
  25. {
  26.     register char    *pchEqual;
  27.     register char    **ppchEntry;
  28.     char        **ppchNewEnv;
  29.     int        nChars;
  30.     int        nNewSize;
  31.  
  32.     if (!pchVar || (pchEqual = strchr(pchVar, '=')) == 0)
  33.         return -1;
  34.     nChars = (int) (pchEqual - pchVar);
  35.  
  36.     /*
  37.      * Note that the BC++4.0 library version also tests for
  38.      * an empty string as a terminating condition. This should
  39.      * not be necessary as _setenv replaces the pointer to
  40.      * the empty string with a NULL pointer.
  41.      */
  42.     for (ppchEntry = _environ;
  43.          *ppchEntry;
  44.          ppchEntry++)
  45.     {
  46.         if (!strncmp(*ppchEntry, pchVar, nChars + 1))
  47.         {
  48.             if (!pchEqual[1])
  49.             {
  50.                 while ((ppchEntry[0] = ppchEntry[1]) != 0)
  51.                     ppchEntry++;
  52.             }
  53.             else
  54.             {
  55.                 *ppchEntry = (char *) pchVar;
  56.             }
  57.             return 0;
  58.         }
  59.     }
  60.     if (!pchEqual[1])
  61.         return 0;
  62.  
  63.     /*
  64.      * Why the -1 here? If we don't do this, we might be overwriting
  65.      * the terminating NULL pointer when there is memory after it that may
  66.      * be uninitialised and/or belong to something else. Then we have the
  67.      * potential to walk off the end of the array.
  68.      */
  69.     if (ppchEntry - _environ > (_envSize / 4) - 1)
  70.     {
  71.         nNewSize = _envSize + sizeof(char *) * 4;
  72.         ppchNewEnv = (char **) malloc(nNewSize);
  73.         memcpy(ppchNewEnv, _environ, _envSize);
  74.         free(_environ);
  75.         ppchEntry = ppchNewEnv + (ppchEntry - _environ);
  76.         _environ = ppchNewEnv;
  77.         _envSize = nNewSize;
  78.     }
  79.  
  80.     ppchEntry[0] = (char *) pchVar;
  81.     ppchEntry[1] = 0;
  82.     return 0;
  83. }
  84.